<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAssign">分配</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleChangeBusiness">转化商机</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="fullName" label="姓名" width="180" sortable>
			</el-table-column>
			<el-table-column prop="phone" label="电话" width="180" sortable>
			</el-table-column>
			<el-table-column prop="seller.username" label="营销人员" width="180" sortable>
			</el-table-column>
			<!--0未分配  1跟进中 2已转化为商机 -1放入线索池-->
			<el-table-column prop="state" label="状态" width="180" sortable>
				<template scope="scope">
					<span v-if="scope.row.state=='0'" style="color: blue">未分配</span>
					<span v-if="scope.row.state=='1'" style="color: orange">跟进中</span>
					<span v-if="scope.row.state=='2'" style="color: green">已转化</span>
					<span v-if="scope.row.state=='-1'" style="color: red">废弃</span>
				</template>
			</el-table-column>
			<el-table-column prop="description" label="描述" min-width="200" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleJoinActivity(scope.$index, scope.row)">参加活动</el-button>
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="query.pageSize" :total="pageInfo.total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增或编辑界面
		:visible.sync="outerVisible" 新版本打开对话框
		v-model="clueFormVisible" 老版本打开对话框
		-->
		<el-dialog title="新增或修改" :visible.sync="clueFormVisible" :close-on-click-modal="false">
			<el-form :model="clue" label-width="80px" :rules="clueFormRules" ref="clueForm">
				<el-form-item label="键" prop="key">
					<el-input v-model="clue.key" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="值" prop="value">
					<el-input v-model="clue.value" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="简介">
					<el-input type="textarea" v-model="clue.intro"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="clueFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="save" :loading="clueLoading">提交</el-button>
			</div>
		</el-dialog>


		<!--
		-->
		<el-dialog title="参与活动" :visible.sync="clueActivityVisible" :close-on-click-modal="false">
			 <!-- 线索id，活动多个id-->
			<el-form :model="clueActivitys" label-width="80px" :rules="clueActivityFormRules" ref="clueActivityForm">
				<el-checkbox-group v-model="clueActivitys.activityIds" >
					<el-checkbox v-for=" a in activityList" :label="a.id">{{a.name}}</el-checkbox>
				</el-checkbox-group>


			<!--	<el-select v-model="clueActivitys.activityIds" multiple clearable placeholder="请选择">
					<el-option
							v-for="item in activityList"
							:key="item.id"
							:label="item.name"
							:value="item.id">
					</el-option>
				</el-select>-->
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="clueActivityVisible = false">取消</el-button>
				<el-button type="primary" @click.native="clueActivitysSubmit">提交</el-button>
			</div>
		</el-dialog>

		<el-dialog title="分配营销人员" :visible.sync="clueAssignVisible" :close-on-click-modal="false">
			<el-form :model="clue" label-width="80px"  ref="clueAssignForm">
				<el-form-item label="姓名">
					<el-input v-model="clue.fullName"  disabled="disabled" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="电话">
					<el-input v-model="clue.phone" disabled="disabled" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="营销人员">
					<el-select v-model="clue.seller" clearable value-key="id" placeholder="请选择">
						<!--下拉选项
						  :key=""  //唯一标识
						  :label 选择之后展示到选择框中的值
						  :value 选中之后绑定给模型层的值  如果要绑定对象给模型层 有一个大坑
							 必须要写  value-key="id"
						-->
						<el-option
								v-for="item in employees"
								:key="item.id"
								:label="item.username"
								:value="item">
							<span style="float: left">{{ item.username }}</span>
							<span style="float: right; color: #8492a6; font-size: 13px">{{ item.email }}</span>
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="clueAssignVisible = false">取消</el-button>
				<el-button type="primary" @click.native="assignSubmit">提交</el-button>
			</div>
		</el-dialog>

		<el-dialog title="转化商机" :visible.sync="clueBusinessVisible" :close-on-click-modal="false">
			<el-form :model="clueBusiness.clue" label-width="80px"  ref="clueBusinessForm">
				<el-form-item label="姓名">
					<el-input v-model="clueBusiness.clue.fullName"  disabled="disabled" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="电话">
					<el-input v-model="clueBusiness.clue.phone" disabled="disabled" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="产品">
					<el-select v-model="clueBusiness.product" clearable value-key="id" placeholder="请选择">
						<!--下拉选项
						  :key=""  //唯一标识
						  :label 选择之后展示到选择框中的值
						  :value 选中之后绑定给模型层的值  如果要绑定对象给模型层 有一个大坑
							 必须要写  value-key="id"
						-->
						<el-option
								v-for="item in products"
								:key="item.id"
								:label="item.name"
								:value="item">
							<span style="float: left">{{ item.name }}</span>
							<span style="float: right; color: #8492a6; font-size: 13px">{{ item.price }}</span>
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="clueBusinessVisible = false">取消</el-button>
				<el-button type="primary" @click.native="clueBusinessSubmit">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
				//分页相关
				query:{
					currentPage:1,
					pageSize:10,
					keyword:''
				},
				pageInfo:{
					total:0,
					rows:[]
				},
				listLoading: false,
				sels: [],//列表选中列

				clueFormVisible: false,//新增界面是否显示
				clueLoading: false,
				clueFormRules: {
					key: [
						{ required: true, message: '请输入键', trigger: 'blur' }
					],
					value: [
						{ required: true, message: '请输入值', trigger: 'blur' }
					]
				},
				clueActivityVisible: false,//新增界面是否显示
				clueActivityFormRules: {
					key: [
						{ required: true, message: '请输入键', trigger: 'blur' }
					],
					value: [
						{ required: true, message: '请输入值', trigger: 'blur' }
					]
				},
				clueAssignVisible: false,//新增界面是否显示
				clueBusinessVisible: false,//新增界面是否显示
				//新增界面数据
				clue: {
					id:null,  //线索相关信息
					fullName: '',
					phone: '',
					seller: {}, //分配营销人员id
				},
				activityList:[],
				clueActivitys:{
					clueId:null,
					activityIds:[]
				},
				employees:[],
				products:[],
				clueBusiness:{
					clue:{},
					product:{}
				}

			}
		},
		methods: {
			//获取用户列表
			getProducts() {
				//发起请求获取数据
				this.$http.get("/product")
						.then(result =>{
							result = result.data; //{success,result:{}}
							if (result.success){
								this.products = result.result;
							}else{
								this.$message({
									message: '获取产品数据失败！'+result.message,
									type: 'error'
								});
							}
						})
						.catch(result=>{
							this.$message({
								message: '系统错误！',
								type: 'error'
							});
						})
			},
			handleChangeBusiness(){
				//获取当前选择的记录给clue
				if (this.sels.length==0 || this.sels.length>1)
				{
					this.$message({
						message: '亲，请选中一个线索后再操作！',
						type: 'error'
					});
					return
				}
				this.clueBusiness.clue = this.sels[0];
				this.getProducts();
				this.clueBusinessVisible = true;
				//修改线索状态
				//线索备注
				//添加线索产品
				//添加商机，-选择一个产品
			},
			clueBusinessSubmit(){
				console.log(this.clueBusiness);
				this.$http.post("/clue/business",this.clueBusiness)
						.then(result=>{
							result = result.data;
							if (result.success){
								//提示成功关闭对话框
								this.$message({
									message: '操作成功！',
									type: 'success'
								});
								this.clueBusinessVisible = false;
								this.getClues(); //刷新表格
							}else{
								this.$message({
									message: '转化失败失败！'+result.message,
									type: 'error'
								});
							}

						})
						.catch(result=>{
							this.$message({
								message: '系统错误！',
								type: 'error'
							});
						})
			},
			//获取用户列表
			getEmployees() {
				//发起请求获取数据
				this.$http.get("/employee")
						.then(result =>{
							result = result.data; //{success,result:{}}
							if (result.success){
								this.employees = result.result;
							}else{
								this.$message({
									message: '获取员工数据失败！'+result.message,
									type: 'error'
								});
							}
						})
						.catch(result=>{
							this.$message({
								message: '系统错误！',
								type: 'error'
							});
						})
			},
			handleAssign(){

				//获取当前选择的记录给clue
				if (this.sels.length==0 || this.sels.length>1)
				{
					this.$message({
						message: '亲，请选中一个线索后再操作！',
						type: 'error'
					});
					return
				}
				this.clue = this.sels[0];

				//弹出对话框
				this.clueAssignVisible = true;
				//获取所有的员工，来选择营销人员
				this.getEmployees();

			},
			assignSubmit(){
				//console.log(this.clueActivitys)
				this.$http.post("/clue/assign",this.clue)
						.then(result=>{
							result = result.data;
							if (result.success){
								//提示成功关闭对话框
								this.$message({
									message: '操作成功！',
									type: 'success'
								});
								this.clueAssignVisible = false;
								this.getClues(); //刷新表格
							}else{
								this.$message({
									message: '分配失败！'+result.message,
									type: 'error'
								});
							}

						})
						.catch(result=>{
							this.$message({
								message: '系统错误！',
								type: 'error'
							});
						})


			},
			clueActivitysSubmit(){
				//console.log(this.clueActivitys)
				this.$http.post("/clue/activitys",this.clueActivitys)
						.then(result=>{
							result = result.data;
							if (result.success){
								//提示成功关闭对话框
								this.$message({
									message: '操作成功！',
									type: 'success'
								});
								this.clueActivityVisible = false;
							}else{
								this.$message({
									message: '获取活动数据失败！'+result.message,
									type: 'error'
								});
							}

						})
						.catch(result=>{
							this.$message({
								message: '系统错误！',
								type: 'error'
							});
						})


			},
			handleJoinActivity:function (index, row) {
				this.clueActivitys.clueId = row.id; //线索id
				//弹出对话框，里面有正在搞的活动，可以进行多选
				this.clueActivityVisible = true;
				//通过对话框选择多个活动 就会有 clueId activitys:[]
				//加载活动信息
				this.$http.get("/activity/legal")
						.then(result =>{
							result = result.data; //{success,result:{}}
							if (result.success){
								this.activityList = result.result;
							}else{
								this.$message({
									message: '获取活动数据失败！'+result.message,
									type: 'error'
								});
							}
							this.listLoading = false;
						})
						.catch(result=>{
							this.$message({
								message: '系统错误！',
								type: 'error'
							});
						})
			},
			search(){
				//重置当前页为第一页
				this.query.currentPage =1;
				this.getClues();
			},
			handleCurrentChange(val) {
				this.query.currentPage= val;
				this.getClues();
			},
			//获取用户列表
			getClues() {
				this.listLoading = true; //发起请求之前显示加载条
				//发起请求获取数据
				this.$http.post("/clue",this.query)
						.then(result =>{
							result = result.data; //{success,result:{}}
							if (result.success){
								this.pageInfo = result.result;
							}else{
								this.$message({
									message: '获取分页数据失败！'+result.message,
									type: 'error'
								});
							}
							this.listLoading = false;
						})
						.catch(result=>{
						this.$message({
								message: '系统错误！',
								type: 'error'
							});
						})
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { id: row.id };
					removeClue(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getClues();
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.clueFormVisible = true;
				this.clue = Object.assign({}, row); //回显

			},
			//显示新增界面
			handleAdd: function () {
				this.clueFormVisible = true;
				this.clue =   {
					id:null,
					key: '',
					value: '',
					intro: '',
				}
			},
			//编辑
			save: function () {
				this.$refs.clueForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.clueLoading = true;
							let para = Object.assign({}, this.clue);
							//把数据转换成我们想要格式 parent: [1, 4]---》parent:{id:4}

							/*if(para.parent){// [2,7]
								para.parent = {"id":para.parent[para.parent.length-1]}
							}*/
							if (para.parent){
								let parentIds = para.parent; // [1, 4]
								let parentId = parentIds[parentIds.length-1]; // 4
								para.parent = {"id":parentId} //parent:{id:4}
							}

							this.$http.put("/clue",para).then((res) => {
								res = res.data;

								if (res.success){
									this.$message({
										message: '保存成功',
										type: 'success'
									});

									this.clueFormVisible = false; //关闭对话框

									this.$refs['clueForm'].resetFields(); //重置表单

									this.getClues(); //重新加载数据
								}else{
									this.$message({
										message: '保存失败,'+res.message,
										type: 'error'
									});
								}

								this.clueLoading = false; //关闭加载条
							});
						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					batchRemoveClue(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getClues();
					});
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getClues();
		}
	}

</script>

<style scoped>

</style>